package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderHistoryBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetOrderHistoryResp;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.common.entitie.OrderHistoryInfo;
import com.ymttest.common.entitie.Ymt_TradingStatus;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取订单历史 FreeMaker Template自动生成代码
 * 已经转接java
 */
@TestSuite
public class Ts_GetOrderHistory {
	private static GetOrderHistoryBean getorderhistorybean;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderHistoryCall getorderhistorycall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单历史");
	}

	@Before
	public void caseUp() {
		getorderhistorybean = new GetOrderHistoryBean();
		getorderhistorycall = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderHistoryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getorderhistorycall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderHistoryResp expectResponse = new GetOrderHistoryResp();
		ConstructExpectedResponse(expectResponse);
		GetOrderHistoryResp actualResponse = getorderhistorycall.getOrderHistoryResp();
		List<com.ymttest.common.entitie.OrderHistoryInfo> expects = expectResponse.getOrderHistoryList();
		List<com.ymttest.common.entitie.OrderHistoryInfo> actuals = actualResponse.getOrderHistoryList();
		if(expects.size()==0){
			DataUtil.verify(0, actuals.size(), "验证OrderHistoryList.size()==0");
		}else{
//			for(com.ymttest.common.entitie.OrderHistoryInfo expect:expects){
//				String expTradingStatusId = expect.getTradingStatusId();
//				boolean exist = actuals.stream().anyMatch(x->x.getTradingStatusId().equals(expTradingStatusId));
//				DataUtil.verify(true, exist, "验证TradingStatusId="+expTradingStatusId+"的记录在OrderHistoryList中存在");
//				if(exist){
//					com.ymttest.common.entitie.OrderHistoryInfo actual = actuals.stream().filter(x->x.getTradingStatusId().equals(expTradingStatusId)).collect(Collectors.toList()).get(0);
//					verifyOrderHistoryInfo(expTradingStatusId, expect, actual);
//				}
//			}
			for(com.ymttest.common.entitie.OrderHistoryInfo expect:expects){
				boolean exist = actuals.stream().anyMatch(x->x.equals(expect));
				DataUtil.verify(true, exist, "验证TradingStatusId="+expect+"的记录在OrderHistoryList中存在");
			}
		}
	}
	
	private void verifyOrderHistoryInfo(String sid, com.ymttest.common.entitie.OrderHistoryInfo expect,com.ymttest.common.entitie.OrderHistoryInfo actual){
		DataUtil.verify(expect.getMemo(), actual.getMemo(),"tradingStatusIdid=[" + sid + "]的Memo");
		DataUtil.verify(expect.getTraddingId(), actual.getTraddingId(),"tradingStatusIdid=[" + sid + "]的TraddingId");
		DataUtil.verifyDateEquals(expect.getUpdateTime(), actual.getUpdateTime(),"tradingStatusIdid=[" + sid + "]的UpdateTime");
		DataUtil.verify(expect.getTradingStatus(), actual.getTradingStatus(),"tradingStatusIdid=[" + sid + "]的TradingStatus");
		DataUtil.verify(expect.getTradingStatusId(), actual.getTradingStatusId(),"tradingStatusIdid=[" + sid + "]的TradingStatusId");
	}

	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResponse(GetOrderHistoryResp response) {
		List<Map> result = execute(getorderhistorybean.getOrderId(), getorderhistorybean.getUserId());
		List<OrderHistoryInfo> OrderHistoryList = new ArrayList<OrderHistoryInfo>();
		if(DataUtil.isNullOrEmpty(result) == false){
			for(Map m:result){
				Ymt_TradingStatus o = new Ymt_TradingStatus();
				DataUtil.mapToBean(o, m);
				OrderHistoryInfo his = new OrderHistoryInfo();
				his.setMemo(o.getmMemo());
				his.setTraddingId(o.getiTraddingId());
				his.setTradingStatus(DataUtil.Integerr.GetValueOrDefault(o.getiTradingStatus()));
//				his.setTradingStatusId(o.getsTradingStatusId());  //志强说： tradingStatusId 这个字段不需要返回
				his.setTradingStatusId("");
				his.setUpdateTime(o.getdUpdateTime());
				OrderHistoryList.add(his);
			}
		}
		response.setOrderHistoryList(OrderHistoryList);
	}
	
	@SuppressWarnings("rawtypes")
	private List<Map> execute(int orderId, int userId){
		return new DefaultWapper().executeSql(GetOrderHistory(orderId, userId));
	}

	public static String GetOrderHistory(int orderId, int userId) {
		String s1 = "declare @rowCount bit = 0 \n";
		String s2 = "select @rowCount = 1"
				+ " from [Ymt_Orders](nolock) where iOrderId = " + orderId
				+ " and (iUserId = " + userId + " or iBuyerId = " + userId
				+ ") \n";
		String s3 = "SELECT [sTradingStatusId], [iTradingStatus]"
				+ ",[dUpdateTime], [mMemo], [iTraddingId]"
				+ " FROM [Ymt_TradingStatus](nolock) "
				+ " where @rowCount = 1 and iTraddingId = " + orderId
				+ " order by dUpdateTime";
		return s1 + s2 + s3;
	}

	public static void main(String[] args) {
		new DefaultWapper().executeSql(GetOrderHistory(112451223, 20226821));
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_001() {
		Logger.start(true, "下单状态的订单，获取订单历史为空");
		try {
			Logger.comment("准备前置数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备前置数据完成");
			// 参数
			getorderhistorybean.setOrderId(orderId);
			getorderhistorybean.setUserId(sellerId);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderHistory_002() {
		Logger.start(false, "订单号为空，获取订单历史失败");
		try {

			// 参数
			// getorderhistorybean.setOrderId(112451223);
			getorderhistorybean.setUserId(20226821);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400, getorderhistorycall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.BCODE8, getorderhistorycall.getBCode(),
					"验证BCode");
			Logger.verifyEquals("orderId:订单号不能为0",
					getorderhistorycall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_003() {
		Logger.start(true, "订单号不存在，获取订单历史为空，成功");
		try {

			// 参数
			getorderhistorybean.setOrderId(123456789);
			getorderhistorybean.setUserId(20226821);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderHistory_004() {
		Logger.start(false, "买家Id为空，获取订单历史失败");
		try {

			// 参数
			getorderhistorybean.setOrderId(112451223);
			// getorderhistorybean.setUserId(20226821);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400, getorderhistorycall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.BCODE8, getorderhistorycall.getBCode(),
					"验证BCode");
			Logger.verifyEquals("userId:用户号不能为0", getorderhistorycall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_005() {
		Logger.start(true, "买家Id不存在，获取订单历史为空，成功");
		try {

			// 参数
			getorderhistorybean.setOrderId(112451223);
			getorderhistorybean.setUserId(123456789);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_006() {
		Logger.start(true, "买家Id不存在和订单号均为空，获取订单历史失败");
		try {

			// 参数
			// getorderhistorybean.setOrderId(112451223);
			// getorderhistorybean.setUserId(123456789);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400, getorderhistorycall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.BCODE8, getorderhistorycall.getBCode(),
					"验证BCode");
			// Logger.verifyEquals("userId:用户号不能为0|orderId:订单号不能为0",
			// getorderhistorycall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_007() {
		Logger.start(true, "买家Id不存在和订单号均不存在，获取订单历史为空，成功");
		try {

			// 参数
			getorderhistorybean.setOrderId(1111111);
			getorderhistorybean.setUserId(1111111);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_008() {
		Logger.start(true, "完成支付状态的订单，获取订单历史成功");
		try {
			Logger.comment("准备前置数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create2_PayOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备前置数据完成");
			// 参数
			getorderhistorybean.setOrderId(orderId);
			getorderhistorybean.setUserId(sellerId);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_009() {
		Logger.start(true, "完成发货状态的订单，获取订单历史成功");
		try {
			Logger.comment("准备前置数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create3_ShippedOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备前置数据完成");
			// 参数
			getorderhistorybean.setOrderId(orderId);
			getorderhistorybean.setUserId(sellerId);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_010() {
		Logger.start(true, "完成退货退款状态的订单，获取订单历史成功");
		try {
			Logger.comment("准备前置数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备前置数据完成");
			// 参数
			getorderhistorybean.setOrderId(orderId);
			getorderhistorybean.setUserId(sellerId);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
//			Logger.comment("getorderhistorycall send:"
//					+ getorderhistorycall.getEntityString());
//			Logger.comment("getorderhistorycall response:"
//					+ getorderhistorycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderHistory_011() {
		Logger.start(true, "买家取消状态的订单，获取订单历史成功");
		try {
			Logger.comment("准备前置数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create12_UserCancelOrder(userId, sellerId).getiOrderId();
			Thread.sleep(500l);
			Logger.comment("准备前置数据完成");
			// 参数
			getorderhistorybean.setOrderId(orderId);
			getorderhistorybean.setUserId(sellerId);
			String s = FormatUtil.beanToHttpString(getorderhistorybean, true,
					false);
			// 发送请求
			getorderhistorycall.setData(s);
			getorderhistorycall.callService();
			Logger.verifyEquals(ECode.SUCCESS, getorderhistorycall.getCode(),
					"验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}